import React from 'react';
import {Table, Row, Col, Form, Input, Icon, InputNumber, Radio, DatePicker, Select, Upload, Button, Switch, Modal, Popconfirm, Tabs, message} from 'antd';
const R = require('ramda');
import BizParams from '../../../components/commons/BizParams'
import CurrencySelect from '../../../components/commons/CurrencySelect'
import EditAgencyagreement from '../components/EditAgencyagreement';
import PersonnelAllocation from '../components/PersonnelAllocation';
import RightBox from "../../commons/RightBox";
import RIGHT from "../../../utils/RightConstants";

const FormItem = Form.Item;
const RadioGroup = Radio.Group;
const Option = Select.Option;
const TabPane = Tabs.TabPane;

export default Form.create({wrappedComponentRef: true})(class Agencyagreement extends React.Component {
    constructor(props) {
        super(props);
        this.state = {
        };
    }

    handleResetFields = () => {
        if(this.personnelAllocation)this.personnelAllocation.handleResetFields();
        this.props.form.resetFields();
    }

    handleOpenAddAgencyagreement = () => {
        let customer = R.isNil(this.props.customer) ? {} : this.props.customer;
        if(customer.csttype === "1"){
            message.info("当前是会员客户，无需增加代理协议");
            return;
        }
        this.editAgencyagreement.handleOpen("add");
    }

    handleOpenPersonnelAllocation = () => {
        this.personnelAllocation.handleOpen();
    }


    onEdit = (record, type) => {
        let model = R.isNil(this.props.model) ? 'customerManage' : this.props.model;
        this.props.dispatch({type: model +'/getAgencyagreementById', payload: {id: record.id}});
        this.editAgencyagreement.handleOpen(type);
    };

    columns = [
        {title: '代理合同协议号', dataIndex: 'agencyno', key: 'agencyno'},
        {title: '签约日期', dataIndex: 'signdate', key: 'signdate'},
        {title: '代理费一美金收费标准(分)', dataIndex: 'agencyfee', key: 'agencyfee'},
        {title: '代理费退税额百分比收费标准（%）', dataIndex: 'percentfee', key: 'percentfee'},
        {title: '外汇押汇比例', dataIndex: 'foreignexratio', key: 'foreignexratio'},
        {title: '客户注意事项', dataIndex: 'cstnote', key: 'cstnote'},
        {title: '所属客户', dataIndex: 'cstName', key: 'cstName'},
        {title: '创建时间', dataIndex: 'createtime', key: 'createtime'},
        {title: '操作', key: 'key_oprator',
              render: (text, record, index) => {
                 return (
                 <span>
                    {this.props.disabled == true ?
                        <span>
                         <a href="#" onClick={this.onEdit.bind(this, record, "view")}>查看</a>
                         </span>
                        :
                         <span>
                         <RightBox resource={RIGHT.CUSTOMER.CUSTOMER_MANAGE_AGENCY_EDIT}><a href="#" disabled={this.props.disabled}          
                              onClick={this.onEdit.bind(this, record, "edit")}>编辑</a>
                            <span className="ant-divider"/></RightBox>

                            <RightBox resource={RIGHT.CUSTOMER.CUSTOMER_MANAGE_AGENCY_DELETE}><Popconfirm placement="leftTop" title={'数据删除后将不可以恢复，是否继续删除？'}
                                 onConfirm={() => {
                                 this.props.dispatch({
                                       type: 'customerManage/deleteAgencyagreementById',
                                        payload: {
                                          id: record.id,
                                          cstid: record.cstid,
                                         }
                                     })
                                 }} okText="是" cancelText="否">
                                 <a href="#" disabled={this.props.disabled} >删除</a>
                             </Popconfirm></RightBox>
                         </span>
                        }

                       </span>
                     );
               }
         }];
                                      
    render() {
        const { getFieldDecorator } = this.props.form;
        let dataSource = R.isNil(this.props.dataSource) ? {} : this.props.dataSource;
        let formItemLayout = {
            labelCol: { xs: { span: 24 }, sm: { span: 6 } },
            wrapperCol: { xs: { span: 24 }, sm: { span: 14 } }
        };
        let values = this.props.form.getFieldsValue();

        let pagination = {
            showTotal: total => `共 ${total} 条数据`,
            pageSize: this.props.agencyagreementPage.size,
            total: this.props.agencyagreementPage.totalRow,
            showSizeChanger: true,
            current: this.props.agencyagreementPage.page,
            onShowSizeChange: (current, size) => {
                let newPage = this.props.agencyagreementPage;
                newPage.size = size;
                newPage.page = 1;
                this.props.dispatch({
                    type: 'customerManage/getAgencyagreementList',
                    payload: {
                        id: this.props.cstid,
                        page: newPage
                    }
                });
            },
            onChange: (pageNumber, pageSize) => {
                let newPage = this.props.agencyagreementPage;
                newPage.page = pageNumber;
                this.props.dispatch({
                    type: 'customerManage/getAgencyagreementList',
                    payload: {
                        id: this.props.cstid,
                        page: newPage
                    }
                });
            }
        }

        return (
            <div>
                {this.props.showPA === true ?
                    <div>
                        <Tabs tabPosition={'top'}>
                            <TabPane tab="人员分配" key="1">
                                <PersonnelAllocation wrappedComponentRef={(inst) => this.personnelAllocation = inst}
                                                     manager = {this.props.manager}
                                                     dispatch={this.props.dispatch} dataSource={this.props.customer}
                                                     disabled={false} cstid={this.props.cstid}/>
                            </TabPane>
                        </Tabs>
                        <Tabs tabPosition={'top'}>
                            <TabPane tab="代理协议" key="1">
                            </TabPane>
                        </Tabs>
                    </div>

                    : ""}

                <Form>
                    <Row>
                        <Col span={12}>
                            <FormItem>
                                <RightBox resource={RIGHT.CUSTOMER.CUSTOMER_MANAGE_AGENCY_ADD}><Button type="primary" icon="plus" size={'default'} disabled={this.props.disabled} onClick={this.handleOpenAddAgencyagreement.bind(this)} style={{display: this.props.disabled ? 'none' : 'inline-block' }}>增加代理协议</Button></RightBox>
                            </FormItem>
                        </Col>
                    </Row>
                </Form>

                <Table dataSource={this.props.agencyagreementList} columns={this.columns} bordered={true} size={'middle'} pagination={pagination} scroll={{x:true}}/>
                <EditAgencyagreement wrappedComponentRef={(inst) => this.editAgencyagreement = inst} dispatch={this.props.dispatch} dataSource={this.props.agencyagreement}
                                     disabled={this.props.disabled} cstid={this.props.cstid}/>

            </div>
        )
    }
})


// WEBPACK FOOTER //
// ./public/src/components/customer/components/Agencyagreement.js